package com.pin.si.www.framework.apidoc;

import com.pin.si.www.framework.apidoc.enums.OpenApiConfigConstants;
import com.pin.si.www.framework.apidoc.properties.OpenApiProperties;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * OpenAPI 配置类
 *
 * @author pin
 */
@Configuration
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = OpenApiConfigConstants.OPENAPI_PREFIX, name = OpenApiConfigConstants.ENABLED, havingValue = OpenApiConfigConstants.HAVING_VALUE_TRUE)
public class OpenApiConfig {

    private final OpenApiProperties openApiProperties;

    @Bean
    public OpenAPI customOpenAPI() {
        var info = openApiProperties.getInfo();
        var contact = openApiProperties.getContact();
        var license = openApiProperties.getLicense();
        var externalDocs = openApiProperties.getExternalDocs();
        
        return new OpenAPI()
                .info(new Info()
                        .title(info.getTitle())
                        .version(info.getVersion())
                        .description(info.getDescription())
                        .contact(new Contact()
                                .name(contact.getName())
                                .email(contact.getEmail())
                                .url(contact.getUrl()))
                        .license(new License()
                                .name(license.getName())
                                .url(license.getUrl())))
                .externalDocs(new ExternalDocumentation()
                        .description(externalDocs.getDescription())
                        .url(externalDocs.getUrl()));
    }
}